﻿using System;
using System.Web.UI.WebControls;

using Gonte.Validation;

namespace Gonte.ObjectModel
{
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class NullableFieldAttribute
        : FieldAttribute
    {
        public NullableFieldAttribute(bool isNullable)
        {
            isNullable_ = isNullable;
        }

        /// <summary>
        /// Whether the field can be null or not
        /// </summary>
        public bool IsNullable
        {
            get { return isNullable_; }
            //set { isNullable_ = value; }
        }

        private bool isNullable_;

        #region FieldAttribute Members

        public override FieldValidator CreateValidator()
        {
            if (isNullable_)
            {
                return new FieldAllwaysValidValidator();
            }

            return new FieldNotNullValidator();
        }

        //public override BaseValidator CreateWebValidationControl()
        //{
        //    if (isNullable_)
        //    {
        //        return null;
        //    }

        //    BaseValidator validator = new RequiredFieldValidator();
        //    if (!string.IsNullOrEmpty(ErrorMessage))
        //    {
        //        validator.ErrorMessage = ErrorMessage;
        //    }

        //    return validator;
        //}

        #endregion
    }
}
